﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using com.hetaoos.FetionSDK;
using Imps.Client.Core;

namespace Fetion_SDK_Demo
{
    public partial class frmMain : Form
    {
        FetionSDK sdk = new FetionSDK();

        public frmMain()
        {
            InitializeComponent();
            sdk.SDK_ReceiveMessage += new FetionSDK.SDK_ReceiveMessageEventHandler(sdk_SDK_ReceiveMessage);
            sdk.SDK_UserSatusChange += new FetionSDK.SDK_UserSatusChangedEventHandler(sdk_SDK_UserSatusChange);

        }

        void sdk_SDK_UserSatusChange(object sender, Imps.Client.Core.UserSatusChangedEventArgs e)
        {
            MessageBox.Show(DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + ":\n\t" + e.NewStatus.ToString());
                
        }

        void sdk_SDK_ReceiveMessage(object sender, com.hetaoos.FetionSDK.Event.SDK_ReceiveMessageEventArgs fe)
        {
            MessageBox.Show( fe.Message,fe.Contact.DisplayName);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            sdk.AccountManager.FillUserIdAndPassword("13800138000", "1234567890123456",true);
            sdk.AccountManager.LoginOrLogout();
            //sdk.AccountManager.CurrentUser.Presence.MainPresence=Imps.Common.MainPresence.
        }

        private void button2_Click(object sender, EventArgs e)
        {
            sdk.ContactControl.SendSMS.SendSMS("13800138000", "This is a Test");
        }


        private void button3_Click(object sender, EventArgs e)
        {
            sdk.ContactControl.SendIM.SendIM("13800138000","Only Test");
            sdk.ContactControl.SendIM.SendIM("1234567890","Only Test2");
            //Contact friend = _contactBase.findContact("15877029295");
            //if (friend != null)
            //{
            //    //ConversationManager.StartConversation(this, ConversationOperation.SmsChat, new string[] { friend.Uri.Raw });
            //}
            //else
            //{
            //    MessageBox.Show("用户为空");
            //}
        }

        private void button4_Click(object sender, EventArgs e)
        {
            bool isByMobileNo = false;
            string mobileNo = string.Empty;
            string domain = string.Empty;
            Nullable<int> targetGroupId = new Nullable<int>();
            string localName = string.Empty;
            ContactList.AddBuddyExtraData extraData = new ContactList.AddBuddyExtraData();
            bool sendRequestAgain = false;
            bool copyWhenExist = false;
            Nullable<bool> invite = new Nullable<bool>();
            AsyncBizOperation op = new AsyncBizOperation("mobileNo");
            op.IsImpsErrorHandled = true;
            op.ImpsError += new EventHandler<ImpsErrorEventArgs>(op_ImpsError);
            op.Successed += new EventHandler(op_Successed);

            isByMobileNo = true;
            mobileNo = txtFriend.Text.ToString();
            localName = "Fetion SDK临时用户";
            targetGroupId = 1;
            string RTXMyName = "我是Fetion SDK会员000";
            invite = new Nullable<bool>();
            extraData = new ContactList.AddBuddyExtraData();
            extraData.WhoAmI = RTXMyName;
            sendRequestAgain = true;
            copyWhenExist = false;
            invite = new Nullable<bool>();

            if (!string.IsNullOrEmpty(mobileNo))
            {
                try
                {
                    sdk.AccountManager.CurrentUser.ContactList.AsyncAddBuddy(isByMobileNo, mobileNo, domain, targetGroupId, localName, extraData, sendRequestAgain, copyWhenExist, invite, op);


                }
                catch
                {
                    MessageBox.Show( mobileNo + " 已存在于好友之中");
                }
            } 
        }

        void op_Successed(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }

        void op_ImpsError(object sender, ImpsErrorEventArgs e)
        {
            //e.
        }
    }
}
